package com.leo.entity;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.ExpireCleanComponent;
import com.almasb.fxgl.dsl.components.LiftComponent;
import com.almasb.fxgl.dsl.components.OffscreenCleanComponent;
import com.almasb.fxgl.dsl.components.ProjectileComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.entity.Spawns;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.leo.component.ArrowComponent;
import com.leo.component.BowComponent;
import com.leo.component.ScoreComponent;
import com.leo.component.ScoreShowComponent;
import com.leo.component.ShootBarComponent;
import com.leo.component.TargetComponent;
import com.leo.constant.ArcheryType;
import javafx.geometry.Point2D;
import javafx.util.Duration;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-06  23:16
 * @Description: TODO
 */
public class ArcheryEntityFactory implements EntityFactory {

    /**
     * 背景
     *
     * @param spawnData
     * @return
     */
    @Spawns("bg")
    public Entity createBg(SpawnData spawnData) {
        return FXGL.entityBuilder(spawnData)
                .view("bg.png")
                .neverUpdated()
                .build();
    }

    /**
     * 弓箭
     *
     * @param spawnData
     * @return
     */
    @Spawns("bow")
    public Entity createBow(SpawnData spawnData) {
        return FXGL.entityBuilder(spawnData)
                .with(new BowComponent())
                .build();
    }

    /**
     * 移动方向
     */
    private static final Point2D MOVE_TO_RIGHT = new Point2D(1, 0);

    /**
     * 箭头
     *
     * @param spawnData
     * @return
     */
    @Spawns("arrow")
    public Entity createArrow(SpawnData spawnData) {
        return FXGL.entityBuilder(spawnData)
                .type(ArcheryType.ARROW)
                .with(new ArrowComponent())
                .with(new ProjectileComponent(MOVE_TO_RIGHT, 600))
                .with(new OffscreenCleanComponent())
                .collidable()
                .build();
    }

    /**
     * 目标
     *
     * @param spawnData
     * @return
     */
    @Spawns("target")
    public Entity createTarget(SpawnData spawnData) {

        // 电梯组件
        LiftComponent liftComponent = new LiftComponent();
        liftComponent.setGoingRight(true);
        liftComponent.yAxisDistanceDuration(180, Duration.seconds(1.8));

        return FXGL.entityBuilder(spawnData)
                .type(ArcheryType.TARGET)
                .with(new TargetComponent())
                // 电梯组件
                .with(liftComponent)
                .collidable()
                .build();
    }

    @Spawns("star")
    public Entity createStar(SpawnData spawnData) {
        return FXGL.entityBuilder(spawnData)
                .view(new AnimatedTexture(new AnimationChannel(FXGL.image("star.png"), Duration.seconds(0.3), 8)).play())
                .with(new ExpireCleanComponent(Duration.seconds(0.3)))
                .build();
    }

    @Spawns("score")
    public Entity createScore(SpawnData spawnData) {
        return FXGL.entityBuilder(spawnData)
                .with(new ScoreComponent())
                .with(new ExpireCleanComponent(Duration.seconds(.8)))
                .build();
    }

    @Spawns("shootBar")
    public Entity createShootBar(SpawnData spawnData) {
        return FXGL.entityBuilder(spawnData)
                .type(ArcheryType.SHOOT_BAR)
                .with(new ShootBarComponent())
                .build();
    }

    @Spawns("cursor")
    public Entity createCursor(SpawnData spawnData) {
        return FXGL.entityBuilder(spawnData)
                .view(new AnimatedTexture(new AnimationChannel(FXGL.image("click.png"), Duration.seconds(0.2), 7)).play())
                .with(new ExpireCleanComponent(Duration.seconds(0.2)))
                .build();
    }

    /**
     * 展示分数
     *
     * @param spawnData
     * @return
     */
    @Spawns("scoreShow")
    public Entity createScoreShow(SpawnData spawnData) {
        return FXGL.entityBuilder(spawnData)
                .type(ArcheryType.SCORE_SHOW)
                .with(new ScoreShowComponent())
                .build();
    }
}
